<?php

namespace Modules\Mp\Http\Controllers\Web;

use App\Http\Controllers\MyController;
use EasyWeChat\Factory;
use Modules\Mp\Models\MpAccountModel;

class WechatServerController extends MyController
{
    public function req($ident)
    {
        $account = MpAccountModel::where('app_id', $ident)->first();

        if ($account) {

            $config = [
                'app_id' => $account->app_id,
                'secret' => $account->app_key,
                'token' => $account->token,
                'response_type' => 'array',
                'aes_key' => $account->aes_key,
            ];

            $wechat = Factory::officialAccount($config);

            $wechat->server->push(function ($message) use ($wechat) {

                switch ($message['MsgType']) {
                    case 'text':
                        break;
                }
            });

            $response = $wechat->server->serve();

            return $response->getContent();
        }
    }

    public function menu($ident)
    {
        $account = MpAccountModel::where('app_id', $ident)->first();

        if ($account) {

            $config = [
                'app_id' => $account->app_id,
                'secret' => $account->app_key,
                'token' => $account->token,
                'response_type' => 'array',
                'aes_key' => $account->aes_key,
            ];

            $wechat = Factory::officialAccount($config);

            $wechat->menu->create([]);
        }
    }
}
